
package guoan.core.filter;

import guoan.core.common.WebConfig;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 系统过滤器
 * 
 * @ClassName: LoginFilter
 * @Description: TODO>
 * @author hao.xu
 * @date 2015年3月31日 上午10:32:13
 * 
 */
public class LoginFilter implements Filter {

    /**
     * 
     * Title：doFilter Description: 所有请求都走此过滤器来判断用户是否登录 user: shaojian.yu date: 2014 2014年11月3日
     * 
     * @param servletRequest
     * @param servletResponse
     * @param filterChain
     * @throws IOException
     * @throws ServletException
     * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest, javax.servlet.ServletResponse, javax.servlet.FilterChain)
     */
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException,
            ServletException{
        // 判断是否是http请求
        if (!(servletRequest instanceof HttpServletRequest) || !(servletResponse instanceof HttpServletResponse)) {
            throw new ServletException("OncePerRequestFilter just supports HTTP requests");
        }
        // 获得在下面代码中要用的request,response,session对象
        HttpServletRequest httpRequest = (HttpServletRequest) servletRequest;
        HttpServletResponse httpResponse = (HttpServletResponse) servletResponse;
        HttpSession session = httpRequest.getSession(true);

        // 进行过滤地址,包括登陆到首页,登陆方法,登出方法
        String[] strs = { "loginPage", "login", "logout", "static" }; // 路径中包含这些字符串的,可以不用登录直接访问
        StringBuffer url = httpRequest.getRequestURL();

        if (strs != null && strs.length > 0) {
            for (String str : strs) {
                if (url.indexOf(str) >= 0) {
                    filterChain.doFilter(servletRequest, servletResponse);
                    return;
                }
            }
        }
        // 从session中获取用户信息
        Object userModel = session.getAttribute(WebConfig.SESSION_USER_KEY);
        if (userModel != null) {
            // 用户存在,可以访问此地址
            filterChain.doFilter(servletRequest, servletResponse);
        } else {
            // 用户不存在,踢回登录页面
            String returnUrl = httpRequest.getContextPath() + "/webCustomer/login";
            httpRequest.setCharacterEncoding("UTF-8");
            httpResponse.setContentType("text/html; charset=UTF-8");
            PrintWriter out = httpResponse.getWriter();
            out.println("<script language=\"javascript\">if(window.opener==null){window.top.location.href=\"" + returnUrl
                    + "\";}else{window.opener.top.location.href=\"" + returnUrl + "\";window.close();}</script>");
        }
        return;
    }

    @Override
    public void init(FilterConfig arg0) throws ServletException{

    }

    @Override
    public void destroy(){

    }
}